package com.shire.shire_interface.service.marketing;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.constants.Constants;
import com.shire.shire_interface.constants.WxConstants;
import com.shire.shire_interface.constants.WxErrorCode;
import com.shire.shire_interface.dao.marketing.WxNewResponseDao;
import com.shire.shire_interface.dao.marketing.WxNewResponseRecordDao;
import com.shire.shire_interface.model.cs.WxChat;
import com.shire.shire_interface.model.friend.WxFriend;
import com.shire.shire_interface.model.marketing.WxNewResponse;
import com.shire.shire_interface.model.marketing.WxNewResponseExample;
import com.shire.shire_interface.model.marketing.WxNewResponseRecord;
import com.shire.shire_interface.model.user.WxAccount;
import com.shire.shire_interface.service.chat.WxChatService;
import com.shire.shire_interface.service.friend.WxFriendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class WxNewResponseService {
    @Resource
    private  WxNewResponseDao responseDao;
    @Resource
    private WxNewResponseRecordDao responseRecordDao;
    @Resource
    private WxChatService chatService;
    @Resource
    private WxFriendService friendService;

    public List<WxNewResponse> getWxNewResponseForAccount(WxAccount account){
        WxNewResponseExample responseExample = new WxNewResponseExample();
        responseExample.createCriteria().andWxaccountEqualTo(account.getWxid()).andStatusEqualTo(0);

        List<WxNewResponse> responseList = responseDao.selectByExample(responseExample);
        return responseList;
    }

    public void checkNewResponse(WxAccount account, WxFriend friend){
        if (friend == null)
            return;
        List<WxNewResponse> responseList = getWxNewResponseForAccount(account);
        for (WxNewResponse response : responseList) {
            if (response == null)
                return;

            boolean flag = false; //是否应答
            if (response.getAddtype() == 0) {
                flag =true;
            }
            else {
                if (friend.getFriendscene() < 99 && response.getAddtype() == 1) {
                    flag = true;
                }else if (response.getAddtype() == 2 && friend.getFriendscene() > 99){
                    flag = true;
                }
            }


            if (flag == false) continue; //不符合条件不处理

            if (response.getUpdatedate() !=null && ((new Date()).getTime() - response.getUpdatedate().getTime()) < (long)(response.getTimeinterval()*1000)){

                continue;
            }

            if (response.getMsgcontent().startsWith("{")) {
                JSONObject jsonObject = JSONObject.parseObject(response.getMsgcontent());
                WxChat chat = new WxChat();
                chat.setWxaccount(account.getWxid());
                chat.setMsgid(String.valueOf((new Date()).getTime()));
                chat.setFriendwxid(friend.getFriendwxid());
                chat.setFriendNick(friend.getFriendnick());
                chat.setAvatar(friend.getAvatar());
                chat.setChatfrom(WxConstants.ChatFrom.SELF.getCode());
                chat.setContent(response.getMsgcontent());
                chat.setMsgtype(jsonObject.getInteger("msgtype"));
                chat.setSendstatus(WxErrorCode.INIT_VALUE);
                chat.setReadstatus(Constants.YesNo.YES.ordinal());
                chat.setFromplat(0);
                chat.setShowstatus(Constants.YesNo.YES.getCode());
                chat.setCsid(account.getCsid());
                chat.setCreatetime(new Date());
                chat.setReadtime(new Date());
                chatService.sendChatByCmd(chat);
                chatService.sendToKf(chat, friend);

                WxNewResponseRecord responseRecord = new WxNewResponseRecord();
                responseRecord.setAvatar(friend.getAvatar());
                responseRecord.setCreatetime(new Date());
                responseRecord.setCsid(account.getCsid());
                responseRecord.setNickname(friend.getFriendnick());
                responseRecord.setRespid(response.getId());
                responseRecord.setWxaccount(account.getWxid());
                responseRecord.setWxid(friend.getFriendwxid());

                responseRecordDao.insert(responseRecord);
            } else {
                JSONArray msgArray = JSONArray.parseArray(response.getMsgcontent());
                for (Object o : msgArray) {
                    JSONObject msgObj = (JSONObject) o;
                    WxChat chat = new WxChat();
                    chat.setWxaccount(account.getWxid());
                    chat.setMsgid(String.valueOf((new Date()).getTime()));
                    chat.setFriendwxid(friend.getFriendwxid());
                    chat.setFriendNick(friend.getFriendnick());
                    chat.setAvatar(friend.getAvatar());
                    chat.setChatfrom(WxConstants.ChatFrom.SELF.getCode());
                    chat.setContent(msgObj.toJSONString());
                    chat.setMsgtype(msgObj.getInteger("msgtype"));
                    chat.setSendstatus(WxErrorCode.INIT_VALUE);
                    chat.setReadstatus(Constants.YesNo.YES.ordinal());
                    chat.setFromplat(0);
                    chat.setShowstatus(Constants.YesNo.YES.getCode());
                    chat.setCsid(account.getCsid());
                    chat.setCreatetime(new Date());
                    chat.setReadtime(new Date());
                    chatService.sendChatByCmd(chat);
                    chatService.sendToKf(chat, friend);
                }
                WxNewResponseRecord responseRecord = new WxNewResponseRecord();
                responseRecord.setAvatar(friend.getAvatar());
                responseRecord.setCreatetime(new Date());
                responseRecord.setCsid(account.getCsid());
                responseRecord.setNickname(friend.getFriendnick());
                responseRecord.setRespid(response.getId());
                responseRecord.setWxaccount(account.getWxid());
                responseRecord.setWxid(friend.getFriendwxid());

                response.setUpdatedate(new Date());
                responseDao.updateByPrimaryKeySelective(response);

                responseRecordDao.insert(responseRecord);
            }
        }
    }

    public int insertSelective(WxNewResponse wxNewResponse) {
        return responseDao.insertSelective(wxNewResponse);
    }

    public int updateSelective(WxNewResponse wxNewResponse){
        return responseDao.updateByPrimaryKeyWithBLOBs(wxNewResponse);
    }

    public List<WxNewResponse> selectByExample(WxNewResponseExample wxNewResponseExample) {

        return responseDao.selectByExample(wxNewResponseExample);
    }

    public int deleteByPrimaryKey(Integer id) {
        return responseDao.deleteByPrimaryKey(id);
    }
}
